from django.db import models
from .utils import BaseModel


# Create your models here.

class UserInfo(BaseModel):
    """用户表"""
    phone = models.CharField(verbose_name='手机号', max_length=11, unique=True)
    token = models.CharField(verbose_name='token', max_length=225, blank=True, null=True)
    bucket = models.CharField(verbose_name='桶', max_length=225, blank=True, null=True)
    region = models.CharField(verbose_name='区域', max_length=32, default='ap-chengdu')
    nickName = models.CharField(verbose_name='微信名称', max_length=225, default='梦')
    avatar = models.CharField(verbose_name='头像', max_length=225,
                              default='19803630852-1645427981-1305448189.cos.ap-chengdu.myqcloud.com/l13c23ek1645803983515.png')
    balance = models.BigIntegerField(verbose_name='余额', default=1000)

    # 缺一个字段，nickName


class Release(BaseModel):
    """发布"""
    position = models.CharField(verbose_name='位置', max_length=225, blank=True, null=True)
    content = models.CharField(verbose_name='内容', max_length=225)

    # 这三个存数量，做一个优化
    release_up = models.BigIntegerField(verbose_name='新闻点赞数量', default=0)
    watch_num = models.BigIntegerField(verbose_name='观看人数数量', default=0)
    release_num = models.BigIntegerField(verbose_name='评论数量', default=0)
    # db_constraint=False db_constraint=False  逻辑上的关联，实质上没有外键联系，增删不会受外键影响，orm查询不影响
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False)
    topic = models.ForeignKey(verbose_name='话题', to='Topic', on_delete=models.DO_NOTHING, db_constraint=False,
                              null=True, blank=True, related_name='topic_release')

    # 图片链接拼接
    @property
    def cover_msg_url(self):
        # 每一个对象走一遍
        msg_url = self.media.all().filter(is_show=True, is_delete=False).first().cosUrl
        # print(msg_url)
        return msg_url


"""
    v1: model进行处理
    # 图片链接拼接
    @property
    def msg_url(self):
        msg_url = self.media.all().filter(is_show=True, is_delete=False).values('cosUrl')
        return msg_url
"""


class Media(BaseModel):
    name = models.CharField(verbose_name='名称', max_length=225, blank=True, null=True)
    choice_type = (
        (1, "无"),
        (2, "图片"),
        (3, "视频"),
    )
    type = models.SmallIntegerField(verbose_name='类型', default=1)
    cosUrl = models.CharField(verbose_name='路径', max_length=225, blank=True, null=True)
    # db_constraint=False,
    release = models.ForeignKey(verbose_name='发布', to='Release', related_name='media', on_delete=models.DO_NOTHING,
                                null=True, blank=True)
    cosETag = models.CharField(verbose_name='校验信息', max_length=225, blank=True, null=True)
    cos_media_name = models.CharField(verbose_name='cos图片名称，删除使用', max_length=225, default='1.png')


class Topic(BaseModel):
    """话题"""
    name = models.CharField(verbose_name='话题名称', max_length=225)
    people_num = models.BigIntegerField(verbose_name='话题关注人数', default=0)

    class Meta:
        verbose_name = "话题"
        verbose_name_plural = "话题"


class Comment(BaseModel):
    """评论表"""
    comment = models.CharField(verbose_name='评论内容', max_length=225, null=True, blank=True)

    new = models.ForeignKey(verbose_name='发布', to='Release', on_delete=models.DO_NOTHING, db_constraint=False,
                            null=True, blank=True, related_name='comment_new')
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='comment_userinfo')
    reply = models.ForeignKey(verbose_name='父评论', to='self', on_delete=models.DO_NOTHING, db_constraint=False,
                              null=True, blank=True, related_name='comment_reply')
    depth = models.PositiveIntegerField(verbose_name='评论深度', default=1)
    root = models.PositiveIntegerField(verbose_name='父id', null=True, blank=True)
    # 优化表中只存储点赞数量，记录在另一个表中
    comment_up = models.BigIntegerField(verbose_name='评论点赞数量', default=0)


class WatchNum(BaseModel):
    """浏览记录表,类似于多对多"""
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='watch_num_userinfo')
    new = models.ForeignKey(verbose_name='发布', to='Release', on_delete=models.DO_NOTHING, db_constraint=False,
                            null=True, blank=True, related_name='watch_num_new')

    def __str__(self):
        return self.userinfo.nickName


class ReleaseUp(BaseModel):
    """浏览记录表,类似于多对多"""
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='ReleaseUp_userinfo')
    new = models.ForeignKey(verbose_name='发布', to='Release', on_delete=models.DO_NOTHING, db_constraint=False,
                            null=True, blank=True, related_name='ReleaseUp_new')


class CommentUp(BaseModel):
    """评论点赞记录表,类似于多对多"""
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='CommentUp_userinfo')
    comments = models.ForeignKey(verbose_name='评论', to='Comment', on_delete=models.DO_NOTHING, db_constraint=False,
                                 null=True, blank=True, related_name='CommentUp_new')


class Auction(models.Model):
    """
    拍卖系列（专场）
    """
    status_choices = (
        (1, '未开拍'),
        (2, '预展中'),
        (3, '拍卖中'),
        (4, '已结束')
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=1)

    title = models.CharField(verbose_name='标题', max_length=32)
    cover = models.FileField(verbose_name='封面', max_length=225)
    video = models.CharField(verbose_name='预览视频', max_length=128, null=True, blank=True)

    preview_start_time = models.DateTimeField(verbose_name='预展开始时间')
    preview_end_time = models.DateTimeField(verbose_name='预展结束时间')

    auction_start_time = models.DateTimeField(verbose_name='拍卖开始时间')
    auction_end_time = models.DateTimeField(verbose_name='拍卖结束时间')
    # 全场保证金
    deposit = models.PositiveIntegerField(verbose_name='全场保证金', default=1000)
    # 当前拍卖场的成交额
    total_price = models.PositiveIntegerField(verbose_name='成交额', null=True, blank=True)
    goods_count = models.PositiveIntegerField(verbose_name='拍品数量', default=0)
    bid_count = models.PositiveIntegerField(verbose_name='出价次数', default=0)
    look_count = models.PositiveIntegerField(verbose_name='围观次数', default=0)

    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    is_operation = models.BooleanField(verbose_name='是否操作过', default=False, help_text='只有未操作过的记录，才可以修改状态')

    class Meta:
        verbose_name_plural = '拍卖系列'

    def __str__(self):
        return self.title


class AuctionItem(models.Model):
    """
    拍卖商品
    """
    auction = models.ForeignKey(verbose_name='拍卖专场', to='Auction', on_delete=models.DO_NOTHING,
                                related_name='auction_item')
    uid = models.CharField(verbose_name='图录号', max_length=225)

    status_choices = (
        (1, '待拍卖'),
        (2, '预展中'),
        (3, '拍卖中'),
        (4, '成交'),
        (5, '流拍'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=1)
    title = models.CharField(verbose_name='拍品名称', max_length=32)
    cover = models.FileField(verbose_name='拍品封面', max_length=225)

    start_price = models.PositiveIntegerField(verbose_name='起拍价')
    deal_price = models.PositiveIntegerField(verbose_name='成交价', null=True, blank=True)

    reserve_price = models.PositiveIntegerField(verbose_name='参考底价')
    highest_price = models.PositiveIntegerField(verbose_name='参考高价')

    video = models.CharField(verbose_name='预览视频', max_length=128, null=True, blank=True)
    # 单品保证金
    deposit = models.PositiveIntegerField(verbose_name='单品保证金', default=100)
    unit = models.PositiveIntegerField(verbose_name='加价幅度', default=100)

    bid_count = models.PositiveIntegerField(verbose_name='出价次数', default=0)
    look_count = models.PositiveIntegerField(verbose_name='围观次数', default=0)

    class Meta:
        verbose_name_plural = '拍品'

    def __str__(self):
        return self.title


class AuctionItemImage(models.Model):
    """
    拍品相关图片
    """
    item = models.ForeignKey(verbose_name='拍品', to='AuctionItem', on_delete=models.DO_NOTHING)
    img = models.FileField(verbose_name='详细图', max_length=255)
    carousel = models.BooleanField(verbose_name='是否在轮播中显示', default=False)
    order = models.FloatField(verbose_name="排序", default=1)

    class Meta:
        verbose_name_plural = '拍品详细图'

    def __str__(self):
        return "{}-{}".format(self.item.title, self.id, )


class AuctionItemDetail(models.Model):
    """
    拍品详细规格
    """
    item = models.ForeignKey(verbose_name='拍品', to='AuctionItem', on_delete=models.DO_NOTHING)
    key = models.CharField(verbose_name='项', max_length=16)
    value = models.CharField(verbose_name='值', max_length=32)

    class Meta:
        verbose_name_plural = '拍品规格'


class BrowseRecord(models.Model):
    """
    商品浏览记录
    """
    item = models.ForeignKey(verbose_name='拍品', to='AuctionItem', on_delete=models.DO_NOTHING)
    user = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING)


class BrowseRecordAuction(models.Model):
    """
    拍卖场浏览记录
    """
    item = models.ForeignKey(verbose_name='拍卖场', to='Auction', on_delete=models.DO_NOTHING, null=True, blank=True)
    user = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, null=True, blank=True)


class BidRecord(models.Model):
    """
    出价记录
    """
    status_choices = (
        (1, '竞价'),
        (2, '成交'),
        (3, '逾期未付款'),
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=1)
    item = models.ForeignKey(verbose_name='拍品', to='AuctionItem', on_delete=models.DO_NOTHING,
                             related_name='bid_record')
    user = models.ForeignKey(verbose_name='出价人', to='UserInfo', on_delete=models.DO_NOTHING)
    price = models.PositiveIntegerField(verbose_name='出价')


class DepositRecord(models.Model):
    """ 保证金 """
    status_choices = (
        (1, '未支付'),
        (2, '支付成功')
    )
    status = models.PositiveSmallIntegerField(verbose_name='状态', choices=status_choices, default=1)

    uid = models.CharField(verbose_name='流水号', max_length=255)
    deposit_type_choices = (
        (1, '单品保证金'),
        (2, '全场保证金')
    )
    deposit_type = models.SmallIntegerField(verbose_name='保证金类型', choices=deposit_type_choices)
    pay_type_choices = (
        (1, '微信'),
        (2, '余额')
    )
    pay_type = models.SmallIntegerField(verbose_name='支付方式', choices=pay_type_choices)

    amount = models.PositiveIntegerField(verbose_name='金额')

    user = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING)

    # 单品保证金则设置值，全场保证金，则为空
    item = models.ForeignKey(verbose_name='拍品', to='AuctionItem', null=True, blank=True,
                             on_delete=models.DO_NOTHING)
    # 都有值
    auction = models.ForeignKey(verbose_name='拍卖专场', to='Auction', on_delete=models.DO_NOTHING)


class Order(models.Model):
    """订单"""
    uid = models.CharField(verbose_name='唯一商品号', max_length=225)
    status_type = (
        (1, '未支付'),
        (2, '支付成功'),
        (3, '逾期未支付'),
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_type, default=1)
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING,
                                 related_name='order_userinfo')
    auction = models.ForeignKey(verbose_name='拍卖场', to='Auction', on_delete=models.DO_NOTHING,
                                related_name='order_auction')

    auction_item = models.ForeignKey(verbose_name='拍卖品', to='AuctionItem', on_delete=models.DO_NOTHING,
                                     related_name='order_auction_item')
    # 点击支付，才有地址
    addr = models.ForeignKey(verbose_name='收获地址', to='Addr', on_delete=models.DO_NOTHING,
                             related_name='order_addr', null=True, blank=True)
    # 默认不使用
    coupon_user = models.ForeignKey(verbose_name='用户优惠劵', to='UserCoupon', on_delete=models.DO_NOTHING, null=True,
                                    blank=True, related_name='order_coupon_user')

    deposit = models.SmallIntegerField(verbose_name='保证金', default=0)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)
    # 成交额度
    price = models.BigIntegerField(verbose_name='成交价')

    r_price = models.BigIntegerField(verbose_name='真实支付价格', default=0)
    coupon_price = models.BigIntegerField(verbose_name='优惠卷价格', default=0)
    pay_type_choices = (
        (1, '微信支付'),
        (2, '余额支付')
    )
    pay_type = models.SmallIntegerField(verbose_name='支付方式',
                                        choices=pay_type_choices, default=1)


class CouponTitle(BaseModel):
    """活动"""
    title = models.CharField(verbose_name='活动', max_length=225)

    def __str__(self):
        return self.title


class CouponType(BaseModel):
    """打折类型"""
    type_range = (
        (1, '满减'),
        (2, '打折'),
        (3, '免费'),
    )
    coupon_type = models.SmallIntegerField(verbose_name='打折类型', default=1, choices=type_range, null=True,
                                           blank=True)
    formula = models.CharField(verbose_name='策略', default='-10', max_length=255)

    def __str__(self):
        return self.get_coupon_type_display()


class CouponStrength(BaseModel):
    """优惠力度"""
    title = models.ForeignKey(verbose_name='活动', to='CouponTitle', on_delete=models.DO_NOTHING,
                              related_name='coupon_title')

    coupon_type = models.ForeignKey(verbose_name='打折类型', to='CouponType', on_delete=models.DO_NOTHING,
                                    related_name='coupon_strength_types')
    type_range = (
        (1, '通用'),
        (2, '专场'),
        (3, '单品'),
    )
    ranges = models.SmallIntegerField(verbose_name='优惠卷范围', default=1, choices=type_range)
    status_type = (
        (1, '未开始'),
        (2, '未过期'),
        (3, '已过期'),
        (4, '删除'),
    )
    status = models.SmallIntegerField(choices=status_type, default=1)
    cover = models.ImageField(verbose_name='优惠劵封面')
    use_limit = models.BigIntegerField(verbose_name='限制', default=100)
    start_time = models.DateTimeField(verbose_name='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间')
    total_count = models.BigIntegerField(verbose_name='总数量', default=100)
    surplus_count = models.BigIntegerField(verbose_name='剩余', default=100)

    def __str__(self):
        return self.get_ranges_display()


class UserCoupon(BaseModel):
    """用户优惠劵"""
    status_type = type_range = (
        (1, '未使用'),
        (2, '已使用'),
        (3, '已过期')
    )
    status = models.SmallIntegerField(verbose_name='使用状态', choices=status_type, default=1)
    user = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, related_name='user_coupon')
    strength = models.ForeignKey(verbose_name='优惠力度', to='CouponStrength', on_delete=models.DO_NOTHING,
                                 related_name='coupon_strength')
    auction_item = models.ForeignKey(verbose_name='拍卖品', to='AuctionItem', on_delete=models.DO_NOTHING,
                                     related_name='coupon_auction_item', null=True, blank=True)
    order_num = models.CharField(verbose_name='订单号', max_length=255, null=True, blank=True)
    end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)


class Addr(models.Model):
    addr = models.CharField(verbose_name='详细地址', max_length=255)
    consignee = models.CharField(verbose_name='收货人', max_length=255)
    phone = models.CharField(verbose_name='手机号码', max_length=255)
    userinfo = models.ForeignKey(verbose_name='用户', to=UserInfo, on_delete=models.DO_NOTHING,
                                 related_name='addr_userinfo')
